package com.lisn.sgseetaface;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.lisn.sgseetaface.widget.SGView;

public class MainActivity extends AppCompatActivity {


    private SGView sgView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermission();
    }

    private int CAMERA_OK_CODE = 111;

    private void init() {
        setContentView(R.layout.activity_main);
        sgView = findViewById(R.id.sgView);
    }

    private void getPermission() {
        if (Build.VERSION.SDK_INT > 22) {
            Activity activity = this;
            if (ContextCompat.checkSelfPermission(activity,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_OK_CODE);
                Log.i("TestOpenGLActivity", "requestPermissions");
            } else {
                Log.i("TestOpenGLActivity", "已经获取了权限");
                init();
            }
        } else {
            Log.i("TestOpenGLActivity", "这个说明系统版本在6.0之下，不需要动态获取权限。");
            init();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CAMERA_OK_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            init();
        }
    }

    public void TakingPhoto(View view) {
        sgView.TakingPhoto();
    }
}
